package ar.com.kique.persistence.dao.impl;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

import ar.com.kique.model.Usuario;
import ar.com.kique.persistence.dao.UsuarioDAO;


public class UsuarioDAOHibernate implements UsuarioDAO {

	private static SessionFactory  sessionFactory = configureSessionFactory();
	private static ServiceRegistry serviceRegistry;

	private static SessionFactory configureSessionFactory() throws HibernateException {
	    Configuration configuration = new Configuration();
	    configuration.configure();
	    serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();        
	    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
	    return sessionFactory;
	}
	  
	@Override
	public Usuario buscarPorNickname(String nickname) {
		Session session = sessionFactory.openSession();
	    Query query = session.createQuery("select u from " + " Usuario u"
	        + " where nickname = :nickname");
	    query.setString("nickname", nickname);
	    return (Usuario) query.uniqueResult();
	}

}
